import { Form } from 'antd-mini/es/Form/form';
import city from '/pages/myziliao/city';

Page({
  data: {
    city,
    toastShow: false,
    radioGroupOptions: [
      { value: '男', label: '男' },
      { value: '女', label: '女' },
    ],
    userAvatar: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ48X_B-1iozuJgHaHa?w=207&h=207&c=7&r=0&o=5&dpr=1.5&pid=1.7',
    openID: '',
    password: '',
    account: '',
    address: '',
    genders: '',
    phone: 'ss',
    email: 'sss',
    isRequesting: false, // 添加一个标志
  },
  onLoad() {
    this.form = new Form();
  },
  handleRef(ref) {
    this.form.addItem(ref);
  },
  async submit() {
    // Check if a request is already in progress
    if (this.data.isRequesting) {
      return;
    }
    this.setData({ isRequesting: true });

    try {
      const values = await this.form.submit();
      console.log(values.genders);
      this.makeRequest(values);
    } catch (error) {
      console.error('Form submission error: ', error);
      this.setData({ isRequesting: false });
    }
  },
  makeRequest(values) {
    my.getStorage({
      key: 'openId',
      success: (res) => {
        const openId = res.data.openId;
        my.request({
          url: 'http://localhost:5657/updateByAliUser',
          method: 'POST',
          data: {
            openID: openId,
            account: values.name,
            password: values.password,
            gender: values.genders
          },
          headers: {
            'content-type': 'application/json',
          },
          dataType: 'json',
          success: (res) => {
            my.alert({ title: "修改成功" });
            my.switchTab({
              url: '/pages/myPage/myPage'
            });
            this.setData({ isRequesting: false });
          },
          fail: (error) => {
            console.error('Request failed: ', JSON.stringify(error));
            this.setData({ isRequesting: false });
          },
        });
      },
      fail: (error) => {
        console.error('Get storage failed: ', JSON.stringify(error));
        this.setData({ isRequesting: false });
      }
    });
  }
});
